
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>页面实时数据监听 · 粥可温-Bug</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-search-pro/search.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-accordion/accordion.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-lightbox/css/lightbox.min.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-splitter/splitter.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-insert-logo/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-tbfed-pagefooter/footer.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-page-toc-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-flexible-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
                
                <link rel="stylesheet" href="../../../gitbook/gitbook-plugin-theme-comscore/test.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../../gitbook/images/favicon.ico" type="image/x-icon">

    
    
    <link rel="prev" href="initVue.html" />
    

    
        <link rel="shortcut icon" href="config/img/book.png" type="image/x-icon">
    
    
        <link rel="bookmark" href="config/img/book.png" type="image/x-icon">
    
    
        <link rel="apple-touch-icon" sizes="152x152" href="config/img/book.png">
    
    
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="config/img/book.png">
    
    
        
        <link rel="apple-touch-icon" sizes="120x120" href="config/img/book.png">
        
        <link rel="apple-touch-icon" sizes="180x180" href="config/img/book.png">
        
    
    
        
        <link rel="apple-touch-icon-precomposed" sizes="120x120" href="config/img/book.png">
        
        <link rel="apple-touch-icon-precomposed" sizes="180x180" href="config/img/book.png">
        
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../../">
            
                <a href="../../../">
            
                    
                    关于我
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <span>
            
                    
                    前端
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="initVue.html">
            
                <a href="initVue.html">
            
                    
                    vue
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.2.2" data-path="页面实时数据监听.html">
            
                <a href="页面实时数据监听.html">
            
                    
                    页面实时数据监听
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <span>
            
                    
                    后端
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../../后端/java.html">
            
                <a href="../../后端/java.html">
            
                    
                    java
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../../后端/wft.html">
            
                <a href="../../后端/wft.html">
            
                    
                    wft
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../../后端/Mybatisplus.html">
            
                <a href="../../后端/Mybatisplus.html">
            
                    
                    mybatisplus
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../../后端/数据库.html">
            
                <a href="../../后端/数据库.html">
            
                    
                    数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../../后端/git.html">
            
                <a href="../../后端/git.html">
            
                    
                    Git
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.6" data-path="../../后端/集合.md">
            
                <span>
            
                    
                    集合
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    其他
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../../不明所以/其他.html">
            
                <a href="../../不明所以/其他.html">
            
                    
                    这到底是什么问题
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../../.." >页面实时数据监听</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x9700;&#x6C42;">&#x9700;&#x6C42;</h2>
<p>&#x65E0;&#x9700;&#x624B;&#x52A8;&#x70B9;&#x51FB;&#x5237;&#x65B0;&#x9875;&#x9762;&#x6570;&#x636E;&#x5B9E;&#x65F6;&#x540C;&#x6B65;&#x66F4;&#x65B0;</p>
<h2 id="&#x5B9E;&#x73B0;&#x60F3;&#x6CD5;&#x4E00;--&#x5B9A;&#x65F6;&#x5668;setintervalwatch">&#x5B9E;&#x73B0;&#x60F3;&#x6CD5;&#x4E00;  &#x5B9A;&#x65F6;&#x5668;setInterval+watch</h2>
<pre><code class="lang-js">setInterval(<span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
          <span class="hljs-keyword">let</span> _that = <span class="hljs-keyword">this</span>;
          setTimeout(
            <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
              _that.getlist(); <span class="hljs-comment">//&#x52A0;&#x8F7D;&#x6570;&#x636E;&#x51FD;&#x6570; &#x81EA;&#x5DF1;&#x7684;&#x65B9;&#x6CD5;</span>
              <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5237;&#x65B0;&quot;</span> + <span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>());
            }, <span class="hljs-number">0</span>);

        }, <span class="hljs-number">1000</span>);

watch&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x76D1;&#x6D4B;Vue&#x5B9E;&#x4F8B;&#x4E0A;&#x7684;&#x6570;&#x636E;&#x53D8;&#x52A8;
</code></pre>
<p>&#x4E8E;&#x662F;&#x53D1;&#x73B0;&#x6BD4;&#x8F83;&#x6D6A;&#x8D39;&#x6027;&#x80FD;&#x5E76;&#x6CA1;&#x6709;&#x8FBE;&#x5230;&#x6211;&#x60F3;&#x8981;&#x7684;&#x9884;&#x671F;&#x6548;&#x679C;</p>
<h2 id="&#x5B9E;&#x73B0;&#x60F3;&#x6CD5;&#x4E8C;--ajax&#x7684;&#x957F;&#x8F6E;&#x8BE2;">&#x5B9E;&#x73B0;&#x60F3;&#x6CD5;&#x4E8C;  ajax&#x7684;&#x957F;&#x8F6E;&#x8BE2;</h2>
<p>Ajax&#x8F6E;&#x8BE2;&#xFF1A;&#x5BA2;&#x6237;&#x7AEF;&#x662F;&#x6309;&#x7167;&#x89C4;&#x5B9A;&#x65F6;&#x95F4;&#x50CF;&#x670D;&#x52A1;&#x7AEF;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#xFF0C;&#x524D;&#x4E00;&#x6B21;&#x8BF7;&#x6C42;&#x5B8C;&#x6210;&#x540E;&#xFF0C;&#x65E0;&#x8BBA;&#x6709;&#x65E0;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#xFF0C;&#x89C4;&#x5B9A;&#x65F6;&#x95F4;&#x4E4B;&#x540E;&#x4E0B;&#x4E00;&#x6B21;&#x8BF7;&#x6C42;&#x53C8;&#x4F1A;&#x53D1;&#x51FA;</p>
<p>Ajax&#x957F;&#x8F6E;&#x8BE2;&#xFF1A;&#x5F53;&#x670D;&#x52A1;&#x7AEF;&#x6536;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x6765;&#x7684;&#x8BF7;&#x6C42;&#x540E;,&#x670D;&#x52A1;&#x7AEF;&#x4E0D;&#x4F1A;&#x76F4;&#x63A5;&#x8FDB;&#x884C;&#x54CD;&#x5E94;&#xFF0C;&#x800C;&#x662F;&#x5148;&#x5C06;&#x8FD9;&#x4E2A;&#x8BF7;&#x6C42;&#x6302;&#x8D77;&#xFF0C;&#x7136;&#x540E;<strong>&#x5224;&#x65AD;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6570;&#x636E;&#x662F;&#x5426;&#x6709;&#x66F4;&#x65B0;</strong>&#x3002;&#x5982;&#x679C;&#x6709;&#x66F4;&#x65B0;&#xFF0C;&#x5219;&#x8FDB;&#x884C;&#x54CD;&#x5E94;&#xFF0C;&#x5982;&#x679C;&#x4E00;&#x76F4;&#x6CA1;&#x6709;&#x6570;&#x636E;&#xFF0C;&#x5219;&#x5230;&#x8FBE;&#x4E00;&#x5B9A;&#x7684;&#x65F6;&#x95F4;&#x9650;&#x5236;(&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x8BBE;&#x7F6E;)&#x624D;&#x8FD4;&#x56DE;&#x3002;  &#x5BA2;&#x6237;&#x7AEF;JavaScript&#x54CD;&#x5E94;&#x5904;&#x7406;&#x51FD;&#x6570;&#x4F1A;&#x5728;&#x5904;&#x7406;&#x5B8C;&#x670D;&#x52A1;&#x5668;&#x8FD4;&#x56DE;&#x7684;&#x4FE1;&#x606F;&#x540E;&#xFF0C;&#x518D;&#x6B21;&#x53D1;&#x51FA;&#x8BF7;&#x6C42;&#xFF0C;&#x91CD;&#x65B0;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x3002;&#x65E2;&#x628A;&#x5FAA;&#x73AF;&#x653E;&#x5230;&#x4E86;&#x670D;&#x52A1;&#x7AEF;</p>
<pre><code class="lang-java">&#x542F;&#x52A8;&#x7C7B;&#x4E0A;&#x5F00;&#x542F;&#x5F02;&#x6B65; <span class="hljs-meta">@EnableAsync</span>

<span class="hljs-meta">@RequestMapping</span>(<span class="hljs-string">&quot;/async&quot;</span>)
<span class="hljs-meta">@RestController</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AsyncRequestDemo</span> </span>{

    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-keyword">private</span> AsyncRequestService asyncRequestService;

    <span class="hljs-meta">@GetMapping</span>(<span class="hljs-string">&quot;/value&quot;</span>)
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">getValue</span><span class="hljs-params">()</span> </span>{

        String msg =  <span class="hljs-keyword">null</span>;
        Future&lt;String&gt; result = <span class="hljs-keyword">null</span>;
        <span class="hljs-keyword">try</span>{
            result = asyncRequestService.getValue();
            msg = result.get(<span class="hljs-number">10</span>, TimeUnit.SECONDS);
        }<span class="hljs-keyword">catch</span> (Exception e){
            e.printStackTrace();
        }<span class="hljs-keyword">finally</span> {
            <span class="hljs-keyword">if</span> (result != <span class="hljs-keyword">null</span>){
                result.cancel(<span class="hljs-keyword">true</span>);
            }
        }

        <span class="hljs-keyword">return</span> msg;
    }

    <span class="hljs-meta">@PostMapping</span>(<span class="hljs-string">&quot;/value&quot;</span>)
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">postValue</span><span class="hljs-params">(String msg)</span> </span>{
        asyncRequestService.postValue(msg);
    }
}

<span class="hljs-meta">@Service</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AsyncRequestService</span> </span>{

    <span class="hljs-keyword">private</span> String msg = <span class="hljs-keyword">null</span>;

    <span class="hljs-meta">@Async</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> Future&lt;String&gt; <span class="hljs-title">getValue</span><span class="hljs-params">()</span> <span class="hljs-keyword">throws</span> InterruptedException </span>{

        <span class="hljs-keyword">while</span> (<span class="hljs-keyword">true</span>){
            <span class="hljs-keyword">synchronized</span> (<span class="hljs-keyword">this</span>){
                <span class="hljs-keyword">if</span> (msg != <span class="hljs-keyword">null</span>){
                    String resultMsg = msg;
                    msg = <span class="hljs-keyword">null</span>;
                    <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> AsyncResult(resultMsg);
                }
            }
            Thread.sleep(<span class="hljs-number">100</span>);
        }
    }

    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">synchronized</span> <span class="hljs-keyword">void</span> <span class="hljs-title">postValue</span><span class="hljs-params">(String msg)</span> </span>{
        <span class="hljs-keyword">this</span>.msg = msg;
    }
}

&#x8FD9;&#x91CC;&#x662F;&#x6839;&#x636E; msg &#x662F;&#x5426;&#x53D8;&#x5316;&#x5224;&#x65AD;&#x662F;&#x5426;&#x54CD;&#x5E94;&#x8FD4;&#x56DE;

<span class="hljs-meta">@EnableAsync</span> &#x5F00;&#x542F;&#x5F02;&#x6B65;
<span class="hljs-meta">@Sync</span> &#x6807;&#x8BB0;&#x5F02;&#x6B65;&#x65B9;&#x6CD5;
Future &#x7528;&#x4E8E;&#x63A5;&#x6536;&#x5F02;&#x6B65;&#x8FD4;&#x56DE;&#x503C;
result.get(<span class="hljs-number">10</span>, TimeUnit.SECONDS); &#x963B;&#x585E;&#xFF0C;&#x8D85;&#x65F6;&#x83B7;&#x53D6;&#x7ED3;&#x679C;
Future.cancel() &#x4E2D;&#x65AD;&#x7EBF;&#x7A0B;
</code></pre>
<p>&#x4E8E;&#x662F;&#x53D1;&#x73B0;&#x5728;&#x591A;&#x7AEF;&#x4E2D;&#x5224;&#x65AD;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x6570;&#x636E;&#x662F;&#x5426;&#x6709;&#x66F4;&#x65B0;&#x81EA;&#x5DF1;&#x53D1;&#x73B0;&#x6BD4;&#x8F83;&#x56F0;&#x96BE;</p>
<h2 id="&#x5B9E;&#x73B0;&#x60F3;&#x6CD5;&#x4E09;-websocket">&#x5B9E;&#x73B0;&#x60F3;&#x6CD5;&#x4E09; websocket</h2>
<p>&#x6536;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x6D88;&#x606F;&#x540E;&#x65E2;&#x5BF9;&#x6570;&#x636E;&#x8BF7;&#x6C42;&#x54CD;&#x5E94;&#xFF0C;&#x6570;&#x636E;&#x7684;&#x5B9E;&#x65F6;&#x540C;&#x6B65;&#x6027;&#x6BD4;&#x8F83;&#x597D;&#xFF0C;</p>
<pre><code class="lang-java">&lt;!-- websocket --&gt;
&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-websocket&lt;/artifactId&gt;
&lt;/dependency&gt;
</code></pre>
<pre><code class="lang-java"><span class="hljs-comment">/**
 * &#x524D;&#x540E;&#x7AEF;&#x4EA4;&#x4E92;&#x7684;&#x7C7B;&#x5B9E;&#x73B0;&#x6D88;&#x606F;&#x7684;&#x63A5;&#x6536;&#x63A8;&#x9001;(&#x81EA;&#x5DF1;&#x53D1;&#x9001;&#x7ED9;&#x81EA;&#x5DF1;)
 *
 * <span class="hljs-doctag">@ServerEndpoint</span>(value = &quot;/ws/one&quot;) &#x524D;&#x7AEF;&#x901A;&#x8FC7;&#x6B64;URI&#x548C;&#x540E;&#x7AEF;&#x4EA4;&#x4E92;&#xFF0C;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;
 */</span>
<span class="hljs-meta">@Slf</span>4j
<span class="hljs-meta">@ServerEndpoint</span>(value = <span class="hljs-string">&quot;/ws/one/{userid}&quot;</span>,encoders = { EncoderClassVo.class })
<span class="hljs-meta">@Component</span>
<span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">OneWebSocket</span> </span>{

    <span class="hljs-comment">/**
     *  &#x4E0E;&#x67D0;&#x4E2A;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8FDE;&#x63A5;&#x5BF9;&#x8BDD;&#xFF0C;&#x9700;&#x8981;&#x901A;&#x8FC7;&#x5B83;&#x6765;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x6D88;&#x606F;
     */</span>
    <span class="hljs-keyword">private</span> Session session;

    <span class="hljs-comment">/**
     * &#x6807;&#x8BC6;&#x5F53;&#x524D;&#x8FDE;&#x63A5;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x7528;&#x6237;&#x540D;
     */</span>
    <span class="hljs-keyword">private</span> String userid;

    <span class="hljs-comment">/**
     *  &#x7528;&#x4E8E;&#x5B58;&#x6240;&#x6709;&#x7684;&#x8FDE;&#x63A5;&#x670D;&#x52A1;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x8FD9;&#x4E2A;&#x5BF9;&#x8C61;&#x5B58;&#x50A8;&#x662F;&#x5B89;&#x5168;&#x7684;
     */</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> ConcurrentHashMap&lt;String,OneWebSocket&gt; webSocketSet = <span class="hljs-keyword">new</span> ConcurrentHashMap&lt;&gt;();

    <span class="hljs-comment">// websocket &#x4E0D;&#x80FD;&#x6CE8;&#x5165;( @Autowired )</span>
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> VdataService vdataService;

    <span class="hljs-meta">@Autowired</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setVdataService</span><span class="hljs-params">(VdataService vdataService)</span> </span>{
        OneWebSocket.vdataService = vdataService;
    }

    <span class="hljs-comment">/**
     * &#x8BB0;&#x5F55;&#x5F53;&#x524D;&#x5728;&#x7EBF;&#x8FDE;&#x63A5;&#x6570;
     */</span>
<span class="hljs-comment">//    private static AtomicInteger onlineCount = new AtomicInteger(0);</span>


    <span class="hljs-comment">/**
     * &#x8FDE;&#x63A5;&#x5EFA;&#x7ACB;&#x6210;&#x529F;&#x8C03;&#x7528;&#x7684;&#x65B9;&#x6CD5;
     */</span>
    <span class="hljs-meta">@OnOpen</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onOpen</span><span class="hljs-params">(Session session, @PathParam(value = <span class="hljs-string">&quot;userid&quot;</span>)</span>String userid) </span>{
        <span class="hljs-keyword">this</span>.session = session;
        <span class="hljs-keyword">this</span>.userid = userid;
        <span class="hljs-comment">// userid&#x662F;&#x7528;&#x6765;&#x8868;&#x793A;&#x552F;&#x4E00;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x5982;&#x679C;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x53D1;&#x9001;&#xFF0C;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x53D1;&#x9001;&#x901A;&#x8FC7;userid&#x6765;&#x533A;&#x5206;</span>
        webSocketSet.put(userid,<span class="hljs-keyword">this</span>);
<span class="hljs-comment">//        onlineCount.incrementAndGet(); // &#x5728;&#x7EBF;&#x6570;&#x52A0;1</span>
        log.info(<span class="hljs-string">&quot;&#x6709;&#x65B0;&#x8FDE;&#x63A5;&#x52A0;&#x5165;&#xFF1A;&#x7528;&#x6237;id&#xFF0C;{}&#xFF0C;&#x5F53;&#x524D;&#x5728;&#x7EBF;&#x4EBA;&#x6570;&#x4E3A;&#xFF1A;{}&quot;</span>,<span class="hljs-keyword">this</span>.userid, webSocketSet.size());

    }

    <span class="hljs-comment">/**
     * &#x8FDE;&#x63A5;&#x5173;&#x95ED;&#x8C03;&#x7528;&#x7684;&#x65B9;&#x6CD5;
     */</span>
    <span class="hljs-meta">@OnClose</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onClose</span><span class="hljs-params">(Session session)</span> </span>{
<span class="hljs-comment">//        onlineCount.decrementAndGet(); // &#x5728;&#x7EBF;&#x6570;&#x51CF;1</span>
        webSocketSet.remove(<span class="hljs-keyword">this</span>.userid);
        log.info(<span class="hljs-string">&quot;&#x6709;&#x4E00;&#x8FDE;&#x63A5;&#x5173;&#x95ED;&#xFF1A;{}&#xFF0C;&#x7528;&#x6237;id&#xFF0C;{}&#xFF0C;&#x5F53;&#x524D;&#x5728;&#x7EBF;&#x4EBA;&#x6570;&#x4E3A;&#xFF1A;{}&quot;</span>, session.getId(),<span class="hljs-keyword">this</span>.userid, webSocketSet.size());
    }

    <span class="hljs-comment">/**
     * &#x6536;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x6D88;&#x606F;&#x540E;&#x8C03;&#x7528;&#x7684;&#x65B9;&#x6CD5;
     *
     * <span class="hljs-doctag">@param</span> message &#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x8FC7;&#x6765;&#x7684;&#x6D88;&#x606F;
     */</span>
    <span class="hljs-meta">@OnMessage</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onMessage</span><span class="hljs-params">(String message)</span> <span class="hljs-keyword">throws</span> ExecutionException, InterruptedException </span>{
        <span class="hljs-comment">// &#x4F60;&#x7684;&#x4E1A;&#x52A1;&#x903B;&#x8F91;</span>
        JSONObject obj = JSONUtil.parseObj(message);
        String userid = obj.get(<span class="hljs-string">&quot;userid&quot;</span>).toString();
        String page = obj.get(<span class="hljs-string">&quot;page&quot;</span>).toString();
        String size = obj.get(<span class="hljs-string">&quot;size&quot;</span>).toString();
        String isend = obj.get(<span class="hljs-string">&quot;isend&quot;</span>).toString();
        System.err.println(obj);
        Future&lt;Object&gt; vData = vdataService.getlistVData(Integer.parseInt(userid), page, size, Integer.parseInt(isend));
        log.info(<span class="hljs-string">&quot;&#x670D;&#x52A1;&#x7AEF; &#x6536;&#x5230;  &#x5BA2;&#x6237;&#x7AEF; [{}]&#x7684;&#x6D88;&#x606F;:{}&quot;</span>, userid, message);
        <span class="hljs-comment">// &#x4F60;&#x7684;&#x4E1A;&#x52A1;&#x903B;&#x8F91;</span>
        <span class="hljs-keyword">this</span>.sendMessage(ResultDTO.success(vData.get()), userid);
    }

    <span class="hljs-meta">@OnError</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">onError</span><span class="hljs-params">(Throwable error)</span> </span>{
        webSocketSet.remove(<span class="hljs-keyword">this</span>.userid);
        log.error(<span class="hljs-string">&quot;&#x53D1;&#x751F;&#x9519;&#x8BEF;&quot;</span>);
        error.printStackTrace();
    }

    <span class="hljs-comment">/**
     * &#x670D;&#x52A1;&#x7AEF;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;
     */</span>
    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">sendMessage</span><span class="hljs-params">(ResultDTO message, String userid)</span> </span>{
        System.err.println(<span class="hljs-string">&quot;userid &quot;</span>+userid);
        <span class="hljs-keyword">try</span> {
            log.info(<span class="hljs-string">&quot;&#x670D;&#x52A1;&#x7AEF; &#x7ED9; &#x5BA2;&#x6237;&#x7AEF;[{}]&#x53D1;&#x9001;&#x6D88;&#x606F;{}&quot;</span>, userid, JSONUtil.parseObj(message));
            webSocketSet.get(userid).session.getBasicRemote().sendObject(message);
        } <span class="hljs-keyword">catch</span> (Exception e) {
            log.error(<span class="hljs-string">&quot;&#x670D;&#x52A1;&#x7AEF;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x5931;&#x8D25;&#xFF1A;{}&quot;</span>, e);
        }
    }
}
</code></pre>
<p>&#x53D1;&#x73B0;&#x670D;&#x52A1;&#x7AEF;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x7ED9;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x53D1;&#x9001;&#x7684;object&#x7C7B;&#x578B;&#x89E3;&#x6790;&#x4E0D;&#x4E86;
&#x9700;&#x8981;&#x914D;&#x7F6E;&#x89E3;&#x6790;&#x7C7B;</p>
<pre><code class="lang-java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EncoderClassVo</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Encoder</span>.<span class="hljs-title">Text</span>&lt;<span class="hljs-title">ResultDTO</span>&gt;</span>{

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">init</span><span class="hljs-params">(EndpointConfig config)</span> </span>{
        <span class="hljs-comment">// TODO Auto-generated method stub</span>

    }

    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">destroy</span><span class="hljs-params">()</span> </span>{
        <span class="hljs-comment">// TODO Auto-generated method stub</span>

    }

    <span class="hljs-comment">//&#x5982;&#x679C;&#x4F60;&#x4F20;&#x9012;&#x7684;&#x662F;&#x4E00;&#x4E2A;&#x7C7B;&#xFF0C;&#x5219;&#x4F7F;&#x7528;&#x5982;&#x4E0B;&#x5199;&#x6CD5;</span>
    <span class="hljs-meta">@Override</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">encode</span><span class="hljs-params">(ResultDTO resultDTO)</span> <span class="hljs-keyword">throws</span> EncodeException </span>{
        <span class="hljs-keyword">return</span> JSONUtil.toJsonStr(resultDTO);
    }
}
</code></pre>
<pre><code class="lang-vue">    destroyed() {
      this.websock.close() //&#x79BB;&#x5F00;&#x8DEF;&#x7531;&#x4E4B;&#x540E;&#x65AD;&#x5F00;websocket&#x8FDE;&#x63A5;
    },
    created() {
      this.initWebSocket();
    },
    initWebSocket(){ //&#x521D;&#x59CB;&#x5316;weosocket
        console.log(&quot;&#x521D;&#x59CB;&#x5316;weosocket&quot;);
        const wsuri = &quot;ws://127.0.0.1:80/ws/one/&quot;+this.userid;
        this.websock = new WebSocket(wsuri);
        this.websock.onopen = this.websocketonopen;
        this.websock.onmessage = this.websocketonmessage;
        this.websock.onerror = this.websocketonerror;
        this.websock.onclose = this.websocketclose;
      },
      websocketonopen(){ //&#x8FDE;&#x63A5;&#x5EFA;&#x7ACB;&#x4E4B;&#x540E;&#x6267;&#x884C;send&#x65B9;&#x6CD5;&#x53D1;&#x9001;&#x6570;&#x636E;
        console.log(&quot;websocket-&#x8FDE;&#x63A5;&#x6210;&#x529F;&quot;)
        let data = {&quot;hi&quot;:&quot;&#x53D1;&#x9001;&#x6570;&#x636E;&quot;};
        this.websocketsend(JSON.stringify(data));
      },
      websocketonerror(){//&#x8FDE;&#x63A5;&#x5EFA;&#x7ACB;&#x5931;&#x8D25;&#x91CD;&#x8FDE;
        this.initWebSocket();
      },
      websocketonmessage(e){ //&#x6570;&#x636E;&#x63A5;&#x6536;
        const redata = JSON.parse(e.data);
        console.log(&quot;ws--&#x6570;&#x636E;&#x63A5;&#x6536;&quot;)
        this.listData = redata.data.records;
        console.log(redata)
      },
      websocketsend(Data){//&#x6570;&#x636E;&#x53D1;&#x9001;
        this.websock.send(Data);
      },
      websocketclose(e){  //&#x5173;&#x95ED;
        console.log(&apos;&#x65AD;&#x5F00;&#x8FDE;&#x63A5;&apos;,e);
      },
</code></pre>
<p><a href="https://kuangstudy.oss-cn-beijing.aliyuncs.com/bbs/2021/02/01/kuangstudy5fcff246-daae-4088-97ca-f628f760313a.png" data-lightbox="6134a410-a83b-4312-b59d-9c6479db456b" data-title="" target="_blank"><img src="https://kuangstudy.oss-cn-beijing.aliyuncs.com/bbs/2021/02/01/kuangstudy5fcff246-daae-4088-97ca-f628f760313a.png" alt=""></a></p>
<p><strong>&#x5728;&#x6709;&#x6570;&#x636E;&#x64CD;&#x4F5C;&#x7684;&#x5730;&#x65B9;&#x53EA;&#x9700;&#x5411;&#x670D;&#x52A1;&#x7AEF;&#x53D1;&#x9001;&#x6D88;&#x606F;&#x5373;&#x53EF; new OneWebSocket().onMessage(msg);</strong></p>
<p>&#x4E8E;&#x662F;&#x5B9E;&#x73B0;&#x4E86;&#x81EA;&#x5DF1;&#x60F3;&#x8981;&#x7684;&#x529F;&#x80FD;&#xFF0C;&#x4EE5;&#x6B64;&#x8BB0;&#x5F55;</p>
<p>2021-02-01 10:36:08 &#x661F;&#x671F;&#x4E00; </p>
<footer class="page-footer"><span class="copyright">Copyright &#xA9; &#x65E0;&#x4EBA;&#x95EE;&#x6211;&#x7CA5;&#x53EF;&#x6E29; all right reserved&#xFF0C;powered by Gitbook</span><span class="footer-modification">&#x6700;&#x540E;&#x66F4;&#x65B0;&#x65F6;&#x95F4;&#xFF1A;
2024-01-18 06:35:38
</span></footer>
                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="initVue.html" class="navigation navigation-prev navigation-unique" aria-label="Previous page: vue">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"页面实时数据监听","level":"1.2.2","depth":2,"next":{"title":"后端","level":"1.3","depth":1,"ref":"","articles":[{"title":"java","level":"1.3.1","depth":2,"path":"bug/后端/java.md","ref":"bug/后端/java.md","articles":[]},{"title":"wft","level":"1.3.2","depth":2,"path":"bug/后端/wft.md","ref":"bug/后端/wft.md","articles":[]},{"title":"mybatisplus","level":"1.3.3","depth":2,"path":"bug/后端/Mybatisplus.md","ref":"bug/后端/Mybatisplus.md","articles":[]},{"title":"数据库","level":"1.3.4","depth":2,"path":"bug/后端/数据库.md","ref":"bug/后端/数据库.md","articles":[]},{"title":"Git","level":"1.3.5","depth":2,"path":"bug/后端/git.md","ref":"bug/后端/git.md","articles":[]},{"title":"集合","level":"1.3.6","depth":2,"path":"bug/后端/集合.md","ref":"bug/后端/集合.md","articles":[]}]},"previous":{"title":"vue","level":"1.2.1","depth":2,"path":"bug/前端/vue/initVue.md","ref":"bug/前端/vue/initVue.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-lunr","-search","search-pro","accordion","lightbox","theme-default","theme-comscore","livereload","highlight","back-to-top-button","splitter","hide-element","code","insert-logo","tbfed-pagefooter","expandable-chapters","chapter-fold","-sharing","sharing-plus","page-toc-button","favicon-absolute","flexible-alerts"],"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"},"pluginsConfig":{"tbfed-pagefooter":{"copyright":"Copyright &copy 无人问我粥可温","modify_label":"最后更新时间：","modify_format":"YYYY-MM-DD HH:mm:ss"},"chapter-fold":{},"styles":{"website":"config/styles/website.css"},"livereload":{},"splitter":{},"search-pro":{},"accordion":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"code":{"copyButtons":true},"favicon-absolute":{"appleTouchIconMore":{"120x120":"config/img/book.png","180x180":"config/img/book.png"},"appleTouchIconPrecomposed152":"config/img/book.png","appleTouchIconPrecomposedMore":{"120x120":"config/img/book.png","180x180":"config/img/book.png"},"favicon":"config/img/book.png","bookmark":"config/img/book.png","appleTouchIcon152":"config/img/book.png"},"hide-element":{"elements":[".gitbook-link"]},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"lightbox":{"jquery":true,"sameUuid":false},"theme-comscore":{},"page-toc-button":{"maxTocDepth":2,"minTocSize":2},"back-to-top-button":{},"flexible-alerts":{"danger":{"className":"danger","icon":"fa fa-ban","label":"Attention"},"note":{"className":"info","icon":"fa fa-info-circle","label":"Note"},"style":"callout","tip":{"className":"tip","icon":"fa fa-lightbulb-o","label":"Tip"},"warning":{"className":"warning","icon":"fa fa-exclamation-triangle","label":"Warning"}},"links":{"sidebar":{"Home":"https://github.com/dunwu/gitbook-notes"}},"sharing":{"qq":true,"qzone":true,"weibo":true,"all":["weibo","qq","qzone"]},"theme-default":{"showLevel":false,"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}},"insert-logo":{"style":"background: none; max-height: 50px; min-height: 50px;border-radius: 50%;","url":"https://portrait.gitee.com/uploads/avatars/user/1617/4852612_MEliauk_1615280065.png!avatar60"},"expandable-chapters":{}},"theme":"default","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"粥可温-Bug","language":"zh-hans","gitbook":"*","description":"这真的不是我的问题啊~~~"},"file":{"path":"bug/前端/vue/页面实时数据监听.md","mtime":"2024-01-18T06:35:38.333Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2024-01-18T06:37:53.061Z"},"basePath":"../../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../../gitbook/gitbook.js"></script>
    <script src="../../../gitbook/theme.js"></script>
    
        
        <script src="../../../gitbook/gitbook-plugin-search-pro/jquery.mark.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-search-pro/search.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-accordion/accordionSelector.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-lightbox/js/lightbox.min.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-splitter/splitter.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-insert-logo/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-expandable-chapters/expandable-chapters.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-page-toc-button/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-flexible-alerts/plugin.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    
        
        <script src="../../../gitbook/gitbook-plugin-theme-comscore/test.js"></script>
        
    

    </body>
</html>

